package j04zuoye;

import java.util.Arrays;
import java.util.Random;

/*
3，平均值
有一个具有5个 int 数据的数组，数组中的每个元素都是[10, 30]之间的随机数。
编写代码求出数组中的元素的和以及元素的平均值，打印出所有高于平均值的数，打印出所有低于平均值的
for(){
  23
}

数
 */
public class J03avg {
    public static void main(String[] args) {
        Random r = new Random();
        //目标数组
        int[] numArr = new int[5];
        //保存和
        int sum = 0;
        //循环生成数组内容
        for (int i = 0; i < numArr.length; i++) {
            numArr[i] = r.nextInt(30-10+1)+10;
            //求和
            sum += numArr[i];
        }
        System.out.println(Arrays.toString(numArr));
        //平均值
        double avg = sum/5.0;
        System.out.println("avg = " + avg);
        //求大值，小值
        String bigStr="";//全部比avg大
        String smallStr="";//全部比avg小
        for (int i = 0; i < numArr.length; i++) {
            if(numArr[i]>avg){
//                System.out.println("比平均值大："+numArr[i]);
                bigStr += numArr[i]+",";
            } else if (numArr[i]<avg) {
//                System.out.println("比平均值小："+numArr[i]);
                smallStr += numArr[i]+",";
            }
        }
        System.out.println("bigStr = " + bigStr);
        System.out.println("smallStr = " + smallStr);
    }
}

class Avg {
    public static void main(String[] args) {
        int[] arr =new int[5];
        Random random = new Random();
        int sum = 0;
        System.out.println("产生的随机数为：");
        for(int i = 0;i <arr.length;i ++){
            arr[i] = random.nextInt(30 - 10 + 1) + 10;
            sum += arr[i];
            System.out.print(arr[i] + " ");
        }
        System.out.println();
        System.out.print("平均数为：" + sum / arr.length);
    }
}
